package com.chen.controller;

import com.chen.pojo.Absence;
import com.chen.service.AbsenceService;
import com.chen.service.StudentService;
import com.chen.service.TeacherService;
import com.chen.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class AbsenceController {
    @Autowired
    AbsenceService absenceService;
    @Autowired
    StudentService studentService;
    @Autowired
    TeacherService teacherService;

    @RequestMapping("/to_absenceList.html")
    public String toAbsenceList(){return "/WEB-INF/pages/admin/absenceList";}

    @RequestMapping("/getAbsenceList.html")
    @ResponseBody
    public PageInfo<Absence> getAbsenceList(String createDate, @RequestParam(defaultValue = "1")Integer pageNum){
        System.out.println("时间："+createDate);
        Absence absence = new Absence();
        absence.setCreateDate(createDate);
        PageHelper.startPage(pageNum,Constants.PAGESIZE);
        List<Absence> absenceList = absenceService.getAbsenceByCreateDate(absence);
        PageInfo<Absence> pageInfo = new PageInfo<Absence>(absenceList);
        for (Absence absence1 : pageInfo.getList()) {
            absence1.setTname(teacherService.getTeacherById(absence1.getTid()).getName());
            absence1.setSname(studentService.getStudentById(absence1.getSid()).getName());
        }
        return pageInfo;
    }

    @RequestMapping("/deleteAbsence.html")
    public String deleteAbsence(Integer id){
        absenceService.deleteAbsenceById(id);
        return "/WEB-INF/pages/admin/absenceList";
    }

}
